const mysql = require("mysql2/promise");

//数据库连接初始化
const InitMySQLDB = async () => {
  const pool = mysql.createPool({
    host: "localhost",
    port: 3307,
    user: "root",
    database: "reactblog",
    password: "123456",
  });
  try {
    //测试连接并释放
    const connection = await pool.getConnection();
    connection.release();
    console.log("MySQL连接成功");
    return pool;
  } catch (error) {
    console.log("MySQL连接失败，请检测参数");
    throw error;
  }
};

/* 
  使用 await global.mysqlPool.query('查询语句')
*/

module.exports = InitMySQLDB;
